package com.kuang.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String[] hobbys = req.getParameterValues("hobbys");
        System.out.println("=========================");
        System.out.println(username);
        System.out.println(password);
        System.out.println(Arrays.toString(hobbys));
        System.out.println("=========================");

        //通过请求转发
        //这里的/会被识别为当前的web应用
        //req.getContextPath() 也代表当前web应用的路径
        req.getRequestDispatcher(req.getContextPath() + "success.jsp").forward(req, resp);
        resp.setCharacterEncoding("utf-8");
    }
    /**
     * 重定向和转发的区别?
     * 相同点
     *      页面都会实现跳转
     *
     * 不同点
     *      请求转发时，url不会产生变化 307
     *      重定向时候, url地址栏会发生变化 302
     */
}
